package com.ias.agdis_r.action.task;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.ias.agdis_r.R;
import com.ias.agdis_r.bean.auth.AuthEnum;
import com.ias.agdis_r.bean.task.Task;
import com.ias.agdis_r.bean.task.TaskProcess;
import com.ias.agdis_r.db.BoxDBHelper;
import com.ias.agdis_r.manager.ActionManager;
import com.ias.agdis_r.manager.DataBaseManager;
import com.ias.agdis_r.manager.DataManager;
import com.ias.agdis_r.manager.JavaBeanManager;
import com.ias.agdis_r.manager.MessageManager;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.request.SubmittaskprocssRequest;
import com.ias.agdis_r.terminalmanager.restful.task.vo.SubmitTaskProcessRequestVO;
import com.ias.agdis_r.util.S;
import com.ias.agdis_r.util.TaskUtil;
import com.ias.agdis_r.util.network.HttpUtil;

import java.util.List;

public class TaskOprateAction extends TaskOprateActionImpl {
	private SubmittaskprocssRequest submittaskprocssRequest;
	private static TaskOprateAction instance;
    private Context context = DataBaseManager.getInstance().getContext();
	/**
	 * 单例模式
	 * 
	 * @return
	 */
	public static TaskOprateAction getInstance() {
		if (instance == null) {
			instance = new TaskOprateAction();
		}
		return instance;
	}

	@Override
	public void onReceiveTask(String tnb) {
		// 根据TNB从接收任务列表中读取TASK
		Task task = DataManager.getInstance().getReceiveTask(tnb);
        // 将TASK保存到工作任务列表中
		DataManager.getInstance().saveWorkTask(tnb, task);
		// 删除接收任务列表中的任务
		DataManager.getInstance().delReceiveTask(tnb);
	}

    @Override
    public void onAcceptTask(String tnb) {
        Task task = null;
        Task temp = DataManager.getInstance().getReceiveTask(tnb);
        Task NFtemp = DataManager.getInstance().getReceiveNFTask(tnb);
        if(null != temp){
            task = temp;
        }else if (null != NFtemp){
            task = NFtemp;
        }
        // 获取要发送的接受信息
        MessageManager.getInstance().setLastRMK("");
        if ("Y".equals(task.getEVENT())){
            initTRPT(tnb, MessageModel.T_EACP, null);
        }else{
            initTRPT(tnb, MessageModel.TACP, null);
        }
        HttpUtil.getInstance().sendRequest(submittaskprocssRequest, MessageModel.TRPT,
                true);
    }

    @Override
    public void onRefuseTask(String tnb) {
        // 获取要发送的拒绝信息
        // 需要设置拒绝原因即进程节点的RMK
        MessageManager.getInstance().setLastRMK(context.getString(R.string.iamDone));
        Task task = DataManager.getInstance().getReceiveTask(tnb);
        if (task == null){
            task = DataManager.getInstance().getReceiveNFTask(tnb);
        }
        if ("Y".equals(task.getEVENT())){
            initTRPT(tnb, MessageModel.T_ERJT, null);
        }else{
            initTRPT(tnb, MessageModel.TRJT, null);
        }
        HttpUtil.getInstance().sendRequest(submittaskprocssRequest, MessageModel.TRPT,
                true);
    }

    @Override
    public void onExcuteTask(String tnb) {
        try {
            Task task = DataManager.getInstance().getWorkTask(tnb);
            if (null == task){
                task = DataManager.getInstance().getWorkNFTask(tnb);
            }
            // 下一任务节点
            TaskProcess nextProcess = TaskUtil.getNextTaskNode(task);
            if (nextProcess != null) {
                // 生成执行任务消息
                initTRPT(tnb, nextProcess.getPCID(), null);
                HttpUtil.getInstance().sendRequest(submittaskprocssRequest,
                        MessageModel.TRPT, true);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }

    private void initTRPT(String tnb, String pid, String rmk) {
        submittaskprocssRequest = new SubmittaskprocssRequest();
        SubmitTaskProcessRequestVO submittaskprocssrequestVO = new SubmitTaskProcessRequestVO();
        submittaskprocssrequestVO.setIFM("");
        submittaskprocssrequestVO.setPID(pid);
        submittaskprocssrequestVO.setRMK(rmk);
        submittaskprocssrequestVO.setSDT(S.getNowTime());
        submittaskprocssrequestVO.setTNB(tnb);
        submittaskprocssrequestVO.setTST("S");
        if (JavaBeanManager.getInstance().adjust_pri(AuthEnum.AI_T_WORKGROUP.toString()) && MessageModel.TEND.equals(pid)) {
            BoxDBHelper boxDBHelper = new BoxDBHelper(DataBaseManager.getInstance().getContext());
            List<SubmitTaskProcessRequestVO.TaskUser> userList = boxDBHelper.getTaskUsers(tnb);
            SubmitTaskProcessRequestVO.TaskUser taskUser = new SubmitTaskProcessRequestVO.TaskUser();
            taskUser.setUsno(JavaBeanManager.getInstance().getUserinfo().getEnb());
            taskUser.setUnam(JavaBeanManager.getInstance().getUserinfo().getEnm());
            userList.add(taskUser);
            submittaskprocssrequestVO.setMembers(userList);
        }
        submittaskprocssRequest
                .setSubmittaskprocssRequestVO(submittaskprocssrequestVO);
    }

	@Override
	public void onExcuteForwardTask(String tnb) {

	}

	@Override
	public void gotoDetailInfoView(String tnb) {
		Handler handler = DataBaseManager.getInstance().getUIHandler();
		Message msg = new Message();
		Bundle data = new Bundle();
		data.putString("serviceId", "gotoDetailInfoView");
		msg.what = ActionManager.UI_UPDATE;
		msg.setData(data);
		handler.sendMessage(msg);
	}
}
